home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15685 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  132 lines

  1. Path: news.cuny.edu!not-for-mail
  2. From: dzielins@its.brooklyn.cuny.edu (Daniel Zielinski)
  3. Newsgroups: comp.lang.c
  4. Subject: QUEUE
  5. Date: 21 Apr 1996 01:10:43 -0400
  6. Organization: Brooklyn College
  7. Message-ID: <4lcg0j$7if@itsop2.its.brooklyn.cuny.edu>
  8. NNTP-Posting-Host: itsop2.its.brooklyn.cuny.edu
  9.  
  10. Can anyone tell me what's wrong with my removeQueue function?
  11. The program compiles, but when I run it, it tells me assert in Queue.c line 31
  12. failed. My test driver fills the Queue with numbers from 1 to 99, but when
  13. I try to get the numbers back from queue (removeQUEUE), it gives me this error.
  14. removeQueue function checks if the queue if it's empty before removing items.
  15. The queue is node based.
  16.  
  17. this is the test driver:
  18.  
  19.  
  20. #include <stdio.h>
  21. #include "node.h"
  22. #include "QUEUE.h"
  23.  
  24. main() {
  25.     Queue q1,q2;
  26.     int i=0;
  27.  
  28.     initQueue(&q1);
  29.     while (i<100) {
  30.         insertQueue(&q1,i);
  31.         i++;
  32.     }
  33.     i=0;
  34.     while(i<100) {
  35.         printf("%d\n",removeQueue(&q1));
  36.         i++;
  37.     }
  38. }
  39.  
  40. this is node.c :
  41.  
  42.  
  43. #include "node.h"
  44. #include <stdio.h>
  45. #include <assert.h>
  46. #include <malloc.h>
  47.  
  48. Node*
  49. newNode(void) {
  50.     Node *np;
  51.  
  52.     np=(Node*)malloc(sizeof(Node));
  53.     assert(np);
  54.     np->next=NULL;
  55.     return np;
  56. }
  57.  
  58. void
  59. deleteNode(Node *np) {
  60.     free(np);
  61. }
  62.  
  63. DataType
  64. getDataNode(Node *np) {
  65.     return np->data;
  66. }
  67.  
  68. Node*
  69. getNextNode(Node *np) {
  70.     return np->next;
  71. }
  72.  
  73. void
  74. setDataNode(Node *np,DataType data) {
  75.     np->data=data;
  76. }
  77.  
  78. void
  79. setNextNode(Node *np,Node *next) {
  80.     np->next=next;
  81. }
  82.  
  83.  
  84. this is the QUEUE.c :
  85.  
  86.  
  87. #include "QUEUE.h"
  88. #include <assert.h>
  89. #include <stdio.h>
  90.  
  91. void
  92. insertQueue(Queue *qp,DataType d) {
  93.     Node *np=newNode();
  94.  
  95.     setDataNode(np,d);
  96.     qp->rear=np;
  97.     if (qp->front==NULL)
  98.         qp->front=np;
  99.     else
  100.         qp->rear->next=np;
  101. }
  102.  
  103. int
  104. emptyQueue(Queue q) {
  105.     assert((q.front==NULL && q.rear==NULL) || (q.front!=NULL && 
  106.         q.rear!=NULL)); 
  107.     return q.front==NULL;
  108. }
  109.  
  110. DataType
  111. removeQueue(Queue *qp) {
  112.     Node *np;
  113.     DataType d;
  114.  
  115.     assert(!emptyQueue(*qp));   <------- THIS IS THE LINE   
  116.     np=qp->front;
  117.     d=np->data;
  118.     qp->front=np->next;
  119.     if (qp->front==NULL)
  120.         qp->rear=NULL;
  121.     deleteNode(np);
  122.     return d;
  123. }
  124.  
  125. void
  126. initQueue(Queue *qp) {
  127.     qp->front=qp->rear=NULL;
  128. }
  129.  
  130.  
  131.  
  132.